<template>
  <div class="page-box">
    <!-- ! 筛选区 -->
    <div class="search-box">
      <el-form size="small" :inline="true" class="demo-form-inline">
        <el-form-item label="角色名">
          <el-input v-model.trim="query.name" placeholder="请输入" style="width:230px" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- ! 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="showDialog = true">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportData">导出</el-button>
        </div>
        <div>
          <el-button icon="el-icon-arrow-down" size="mini" circle />
          <el-button icon="el-icon-refresh" size="mini" circle />
          <el-button icon="el-icon-menu" size="mini" circle />
        </div>
      </el-row>
    </div>
    <!-- ! 表格区域 -->
    <div class="table-box">
      <el-table
        :data="list"
        style="width: 100%"
        border
      >
        <el-table-column type="index" label="#" width="50" />
        <el-table-column prop="id" label="角色ID" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="description" label="角色描述" />
        <el-table-column prop="create_date" label="创建时间" />
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-row type="flex" justify="center">
              <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="editRoles(row.id)" />
              <el-button circle type="danger" icon="el-icon-delete" size="mini" />
            </el-row>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <!-- ! 分页区域 -->
    <div class="page">
      <el-row type="flex" justify="end">
        <el-pagination
          background
          :current-page="query.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="query.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- ! 弹层 -->
    <el-dialog width="700px" title="添加角色" :visible.sync="showDialog" @close="close">
      <!-- 表单内容 -->
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" size="mini" style="width:500px" />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="roleForm.description" style="width:500px" type="textarea" :rows="3" size="mini" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-tree
            ref="tree"
            check-strictly
            node-key="id"
            :data="treeList"
            :default-checked-keys="roleForm.menuids"
            :props="{label:'title'}"
            show-checkbox
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="submit">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getMenuList } from '@/api/system/menu'
import { getRoleList, addRole, getRoleDetail } from '@/api/system/role'
// ! 导出
import { exportData } from '@/utils/export'

export default {
  name: 'Role',
  data() {
    return {
      treeList: [],
      roleForm: {
        name: '',
        description: '',
        menuids: []
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      // ! 导出数据
      aoa: [],
      list: [],
      total: 0,
      query: {
        name: '',
        limit: 10,
        page: 1
      },
      showDialog: false
    }
  },
  created() {
    this.initData()
    this.initTreeData()
  },
  methods: {
    // ! 编辑数据回显
    async editRoles(id) {
      const res = await getRoleDetail(id)
      console.log(res)
      this.roleForm = res
      this.showDialog = true
    },
    // ! 添加&编辑角色
    async submit() {
      this.$refs.roleForm.validate(async isOk => {
        if (!isOk) return
        this.roleForm.menuids = this.$refs.tree.getCheckedKeys()
        await addRole(this.roleForm)
        this.$message.success('添加成功')
        this.initData()
        this.close()
      })
    },
    // ! 关闭弹层
    close() {
      this.roleForm = {
        name: '',
        description: '',
        menuids: []
      }
      this.$refs.roleForm.clearValidate()
      this.showDialog = false
    },
    // ! 获取树形结构
    async  initTreeData() {
      const res = await getMenuList()
      this.treeList = res
    },
    // ! 导出
    exportData() {
      this.list.forEach((index, item) => {
        this.aoa.push([index + 1, item.id, item.name, item.description, item.create_date])
      })
      exportData(this.list, ['#', '角色ID', '角色名称', '角色描述', '创建时间'], this.aoa)
    },

    // ! 搜索
    search() {
      this.query.page = 1
      this.initData()
    },
    // ! 重置
    reset() {
      this.query = {
        name: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // ! 分页
    handleSizeChange(value) {
      this.query.limit = value
      this.query.page = 1
      this.initData()
    },
    handleCurrentChange(value) {
      this.query.page = value
      this.initData()
    },
    // ! 获取分页列表信息
    async initData() {
      const res = await getRoleList(this.query)
      this.list = res.data
      this.total = res.count
    }
  }
}
</script>
<style lang="scss" scoped>
  .page-box {
    margin: 20px;
    padding: 20px;
    background-color: #fff;
    .search-box,.tabbar-box {
      padding-top: 10px;
      height: 50px;
    }

  }
  ::v-deep .el-dialog__header{
   padding: 15px 20px 10px!important;
    border-bottom: 1px solid #e9ecef;
    background-color: #fafafa;
  }

</style>
